﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KPIS.GERP.Models.Class
{
    public static class CodeGenerate
    {
        public enum TransectionType
        {
            PrePurchaseRequest,
            PurchaseRequest,
            PurchaseOrder,
            Bid,
            Bidder,
            DeliveryNote,
            Dispense,
            ReceiveParcel,
            RequestParcel,
            InventoryStockTaking,
            MoneyRequest,
            Task,
            Absence,
            Dispose,
        }

        public static string GetNextCode(TransectionType type, string strBgmSourceNo)
        {
            int buddishtYear = new ThaiBuddhistCalendar().GetYear(DateTime.Now);
            string strCodeDate = buddishtYear.ToString("0000").Substring(DateTime.Today.Year.ToString("0000").Length - 2)
                + DateTime.Today.Month.ToString("00");
            string strResult = "";
            string strPrefix = "";
            string strMaxCode = "";

            using (var db = new GERPContext())
            {
                switch (type)
                {
                    case TransectionType.PrePurchaseRequest:
                        strPrefix = "PP";
                        strMaxCode = db.PrePurchaseRequests.ToList().Where(m => (m.Code ?? "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.Code);
                        break;
                    case TransectionType.PurchaseRequest:
                        strPrefix = "PR";
                        strMaxCode = db.PurchaseRequests.ToList().Where(m => (m.Code ?? "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.Code);
                        break;
                    case TransectionType.PurchaseOrder:
                        strPrefix = "PO";
                        strMaxCode = db.PurchaseOrders.ToList().Where(m => (m.Code ?? "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.Code);
                        break;
                    case TransectionType.Bid:
                        strPrefix = "BI";
                        strMaxCode = db.Bids.ToList().Where(m => (m.Code ?? "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.Code);
                        break;
                    case TransectionType.Bidder:
                        strPrefix = "BD";
                        //strMaxCode = db.Bidders.ToList().Where(m => (m.Code != null ? m.Code : "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.Code);
                        break;
                    case TransectionType.DeliveryNote:
                        strPrefix = "DN";
                        strMaxCode = db.DeliveryNotes.ToList().Where(m => (m.Code ?? "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.Code);
                        break;
                    case TransectionType.Dispense:
                        strPrefix = "DP";
                        // strMaxCode = db.Dispenses.ToList().Where(m => (m.Code != null ? m.Code : "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.Code);
                        break;
                    case TransectionType.ReceiveParcel:
                        strPrefix = "RC";
                        strMaxCode = db.Receiveds.ToList().Where(m => (m.ReceiveCode ?? "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.ReceiveCode);
                        break;
                    case TransectionType.RequestParcel:
                        strPrefix = "RP";
                        strMaxCode = db.RequestParcels.ToList().Where(m => (m.IcmReqCode ?? "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.IcmReqCode);
                        break;
                    case TransectionType.InventoryStockTaking:
                        strPrefix = "TK";
                        strMaxCode = db.InventoryStockTakings.ToList().Where(m => (m.StockTakingCode ?? "").StartsWith(strPrefix + strCodeDate)).Max(m => m.StockTakingCode);
                        break;
                    case TransectionType.MoneyRequest:
                        strPrefix = "RQ";
                        strMaxCode = db.PayRequests.ToList().Where(m => (m.Code ?? "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.Code);
                        break; 
                    case TransectionType.Task:
                        strPrefix = "TS";
                        strMaxCode = db.Tasks.ToList().Where(m => (m.Code ?? "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.Code);
                        break;
                    case TransectionType.Absence:
                        strPrefix = "AB";
                        strMaxCode = db.PsAbsences.ToList().Where(m => (m.Code ?? "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.Code);
                        break;
                    case TransectionType.Dispose:
                        strPrefix = "DI";
                        strMaxCode = db.Disposes.ToList().Where(m => (m.AssetCode ?? "").StartsWith(strPrefix + strCodeDate)).Max(m => m.AssetCode);
                        break;
                }
            }

            if (string.IsNullOrWhiteSpace(strMaxCode))
            {
                strResult = strPrefix + strBgmSourceNo + strCodeDate + "0001";
            }
            else
            {
                var strRunning = strMaxCode.Substring(strMaxCode.Length - 4);
                int nRunning;
                if (int.TryParse(strRunning, out nRunning))
                {
                    strResult = strPrefix + strBgmSourceNo + strCodeDate + (nRunning + 1).ToString("0000");
                }
                else
                {
                    strResult = strPrefix + strBgmSourceNo + strCodeDate + "0001";
                }
            }
            
            return strResult;
        }

        public static string GetNextCode(TransectionType transectionType)
        {
            return GetNextCode(transectionType, "");
        }
    }
}
